package Server.lastproject;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import Server.MyDataType.MyDataDynamic;
import Server.SQL.CommandManage;
import java.net.Socket;
import java.io.*;
/**
 *
 * @author Admin
 */
public class ThreadSocket extends Thread{
    Socket socket= null;
  
   
   
    public ThreadSocket(Socket socket)
    {
        this.socket=socket;
        System.out.print("may con da conect vao");
        start();

    }

   
    @Override
    public void run()
    {
        Boolean Continue = true;
        ObjectOutputStream oos = null; //Used to write an object to the streem
        BufferedReader fromClient=null;
        try {
            oos = new ObjectOutputStream(socket.getOutputStream());
             fromClient = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        while (Continue) {
            try {
                
               // DataOutputStream sendToClient = new DataOutputStream(socket.getOutputStream());// Tao output stream
                //Tao input stream

                String query = fromClient.readLine();// Chuỗi nhận được từ Client
                //fromClient.reset();
                System.out.print(query);
                CommandManage cmd = new CommandManage(query);
                //Boolean isNonQuery =  CommandManage.isNonQuery(query);
                String type = CommandManage.getType(query);
                MyDataDynamic result= new MyDataDynamic();

                result = cmd.Analysis();
                if(result != null)
                {
                    if(!result._vtNameColumn.isEmpty())
                    {
                            oos.writeObject(type+"#thanh cong");                   
                            oos.writeObject(result._vtNameColumn);
                            oos.writeObject(result.getObject());
                    }
                    else
                    {
                        if("select".equals(type))                                              
                            oos.writeObject(type+"#that bai");
                        else
                            oos.writeObject(type+"#thanh cong");
                    }
                }
                else
                {
                    oos.writeObject(type+"#that bai");
                }
            } catch (Exception e) {
                Continue = false;
            }
        }
    }

   
    
}
